﻿using System;
using System.Reflection;
using EPiServer.DataAbstraction;
using EPiSugar.EntityAdapter.PropertyBinders;

namespace EPiSugar.EntityAdapter
{
    /// <summary>
    /// Binds to default properties of the PageData object.
    /// </summary>
    public class BindToAttribute : Attribute, IBinderFactory
    {
        private readonly PageProperty getDataFrom;

        /// <summary>
        /// Select the default PageData property to use.
        /// </summary>
        /// <param name="getDataFrom">The particular property to copy.</param>
        public BindToAttribute(PageProperty getDataFrom)
        {
            this.getDataFrom = getDataFrom;
        }

        public IPropertyBinder CreateBinder(PageDefinitionCollection propertyDefinitions, PropertyInfo destinationProperty)
        {
            if (getDataFrom == PageProperty.Children)
                return new LazyChildBinder(destinationProperty, Current.PageSource);
            return new PagePropertyBinder(getDataFrom, destinationProperty);
        }
    }
}
